package com.wu.dms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wu.dms.cache.UrlCache;
import com.wu.common.convertion.result.Result;
import com.wu.common.convertion.result.Results;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.dms.dao.entity.SongList;
import com.wu.dms.dao.mapper.SongListMapper;
import com.wu.dms.dto.SongListRequestDTO;
import com.wu.dms.dto.resp.PageResp;
import com.wu.dms.service.SongListService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author EDY
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("songList")
public class SongListController {


    private final SongListService songListService;

    private final SongListMapper songListMapper;


    // 添加歌单
    @PostMapping("/add")
    public Result<Boolean> addPlayList(@RequestBody SongListRequestDTO addSongListRequestDTO) {
        songListService.addPlayList(addSongListRequestDTO);
        return Results.success(true);
    }

    // 删除歌单
    @GetMapping("/delete")
    public Result<Boolean> deletePlayList(@RequestParam Long id) {
        songListService.deletePlayList(id);
        return Results.success(true);
    }




    // 返回标题包含文字的歌单
    @GetMapping("/likeTitle/detail")
    public Result<List<SongList>> playListOfLikeTitle(@RequestParam String title) {
        return Results.success(songListService.likeTitle(title));
    }

    // 返回指定类型的歌单
    @GetMapping("/style/detail")
    public Result<List<SongList>> playListOfStyle(@RequestParam String style) {
        return Results.success(songListService.likeStyle(style));
    }

    // 更新歌单信息
    @PostMapping("/update")
    public Result<Boolean> updatePlayListMsg(@RequestBody SongListRequestDTO updateSongListRequestDTO) {
        songListService.updatePlayListMsg(updateSongListRequestDTO);
        return Results.success(true);

    }

    // 更新歌单图片
    @PostMapping("/img/update")
    public Result<Boolean> updatePlayListPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") Long id) {
        songListService.updatePlayListImg(avatorFile, id);
        return Results.success(true);
    }
    @GetMapping
    public Result<PageResp> querySingerInPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String songListName) {
        Page<SongList> page = Page.of(pageNum, pageSize);
        Page<SongList> songListPage = songListService.query().like( "title", songListName).page(page);
        List<SongList> songList = songListPage.getRecords();
        List<SongList> collect = songList.parallelStream()
                .peek(song -> {
                    String imageUrl = UrlCache.getUrl(MinIoNameEnum.SONG_LIST_IMAGE.getBucketName(), song.getId(),song.getPic());
                    song.setPic(imageUrl);
                }).collect(Collectors.toList());
        PageResp pageResp = new PageResp();
        pageResp.setTotal(songListPage.getTotal());
        pageResp.setData(collect);
        return Results.success(pageResp);

    }

}
